<?php
class PTControl extends CI_Controller {
function __construct()
 {
   parent::__construct();
//   $config['is_ajax_paging']    = TRUE; //default FALSE
	$this->load->library('javascript');
	$this->load->model('PTdomainModel');
	$this->load->model('kuesionerModel');
	$this->load->model('normModel');
	$this->load->model('normDomainModel');
	$this->load->model('normFacetModel');
	$this->load->model('PTdomainModel');
	$this->load->model('PTfacetModel');
	$this->load->model('usiaUserModel');
	$this->load->model('penjelasandomainmodel');
	$this->load->model('penjelasanfacetmodel');
 }
	
	function getJawaban()
	{
	if($this->input->post('submit'))
		{
			$question = $this->kuesionerModel->getKuesioner()->result();
			for ($i = 1; $i <= count($question) ; $i++) {
//				$Q[$i] = $this->input->post('Q'.$i.'');
				if( isset($_POST['Q'.$i]) ){
					$Q[$i] = $_POST['Q'.$i];
					if (($question[$i-1]->KEY_) == '-') {
						$Q[$i] = 6 - $Q[$i];
					}
				} else {
//					echo "Semua pertanyaan harus dijawab";
					redirect('kuesionerControl/index');
				}
				
			}
//			echo  12345;
//			echo $this->input->post("Q1");
			return $Q;  		
		}
	}


	function scoringPT(){
		$session_data = $this->session->userdata('logged_in');
     	$userid = $session_data['userid'];
     	$username = $session_data['username'];
     	$gender = $session_data['gender'];
     	$usia = $this->usiaUserModel->getUsia($userid)->result();
		if (count($usia) > 1) {
     		for ($j2 = 0; $j2 < count($usia); $j2++) {
     			$age[$j2] = $usia[$j2]->USIAID;
     		}
     		$usiaid = max($age);
     	} else {
     		$usiaid = $usia[0]->USIAID;
     	}
     	
		$answer = $this->getJawaban();
		
		for ($sf = 1; $sf <= 30; $sf++) {  //menjumlahkan nilai masing-masing facet dari 4 pertanyaan
	    	$sumfacet[$sf] = 0;
	    	for ($j = 0; $j < 4; $j++) { 
	    		$sumfacet[$sf] = $sumfacet[$sf] + $answer[$sf+($j*30)];
			}
		}
		
		for ($sd = 1; $sd <= 5 ; $sd++) {  //menjumlahkan nilai masing-masing facet dari 6 jumlah nilai facet
			$sumdomain[$sd] = 0;
			for ($i = 0; $i < 6; $i++) {
				$sumdomain[$sd] = $sumdomain[$sd] + $sumfacet[$sd+($i*5)];
			}
		}
		
		$cube1 = 210.335958661391;
		$cube2 = 16.7379362643389;
		$cube3 = 0.405936512733332;
		$cube4 = 0.00270624341822222;
		$high = 55;
		$low = 45;
		$norms = $this->normModel->getNorm($usiaid, $gender)->result(); //1 = usia, F = gender, ntar diganti parameternya
		for ($d = 1; $d <= 5; $d++) {  
			//untuk menghitung skor domain :
			$ndomain[$d] = $this->normDomainModel->getNormDomain($norms[0]->NORMID, $d)->result();
			$domain[$d] = 50 + (10 * ($sumdomain[$d]-$ndomain[$d][0]->MEANDOMAIN)/$ndomain[$d][0]->SDDOMAIN);
			//untuk menentukan level domain :
			if ($domain[$d] < $low) {
			   $leveld[$d] = "low"; 
			}
			if ($domain[$d] >= $low && $domain[$d] <= $high) {
			   $leveld[$d] = "average";
			}
			if ($domain[$d] > $high) {
			   $leveld[$d] = "high";
			}
			//untuk menghitung nilai persentil domain :
			$persendomain[$d] = floor($cube1 - ($cube2 * $domain[$d]) + ($cube3 * (pow($domain[$d], 2))) - ($cube4 * pow($domain[$d], 3)));
			if ($domain[$d] < 27) {$persendomain[$d] =  1;}
			if ($domain[$d] > 73) {$persendomain[$d] =  99;}
			$this->PTdomainModel->insertPTDomain($usiaid,$userid,$d,$persendomain[$d],$leveld[$d]); //$usiaid,$userid,$domainke,$nilaidomain,$leveldomain
		}
		
		for ($f = 1; $f <= 30; $f++) { 
			//untuk menghitung skor facet :
			$nfacet[$f] = $this->normFacetModel->getNormFacet($norms[0]->NORMID, $f)->result();
			$facet[$f] = 50 + (10 * ($sumfacet[$f]-$nfacet[$f][0]->MEANFACET)/$nfacet[$f][0]->SDFACET);
			//untuk menentukan level facet :
			if ($facet[$f] < $low) {
			   $levelf[$f] = "low"; 
			}
			if ($facet[$f] >= $low && $facet[$f] <= $high) {
			   $levelf[$f] = "average";
			}
			if ($facet[$f] > $high) {
			   $levelf[$f] = "high";
			}
			//untuk menghitung nilai persentil facet :
			$persenfacet[$f] = floor($cube1 - ($cube2 * $facet[$f]) + ($cube3 * (pow($facet[$f], 2))) - ($cube4 * pow($facet[$f], 3)));
			if ($facet[$f] < 27) {$persenfacet[$f] =  1;}
			if ($facet[$f] > 73) {$persenfacet[$f] =  99;}
								
		}
		$dmn = $this->PTdomainModel->getPTDomain($userid, $usiaid)->result();
		for ($k = 0; $k < count($dmn); $k++) {
				for ($k2 = 1; $k2 <= 6; $k2++) {
//					echo $dmn[$k]->PTDOMAINID." : ";
//					echo (($k*6)+$k2)." | ";
					$this->PTfacetModel->insertPTFacet($dmn[$k]->PTDOMAINID,(($k*6)+$k2),$persenfacet[(($k*6)+$k2)],$levelf[(($k*6)+$k2)]);
				}
				
			}
		
		
//		$this->load->view('tesreko');
//		redirect('PTControl/getJawaban1','refresh');
		redirect('similarityControl/similarityGenre','refresh');
//			echo $userid;
////			echo $gender;
////			echo $username;
////			echo  count($usia);
////			echo $usiaid;
//			echo "bismillah..success!";
//			$this->load->view('tesreko');
		
	}
	
	function resultPT(){
		$session_data = $this->session->userdata('logged_in');
     	$userid = $session_data['userid'];
     	$username = $session_data['username'];
     	$gender = $session_data['gender'];
     	$usia = $this->usiaUserModel->getUsia($userid)->result();
		if (count($usia) > 1) {
     		for ($j2 = 0; $j2 < count($usia); $j2++) {
     			$age[$j2] = $usia[$j2]->USIAID;
     		}
     		$usiaid = max($age);
     	} else {
     		$usiaid = $usia[0]->USIAID;
     	}
		$detail['dom'] = $this->PTdomainModel->getPTDomain($userid, $usiaid)->result();
		for ($k = 0; $k < count($detail['dom']); $k++) {
//			function getPenjelasanDomain($domainke)
			$detail['penjdom'][$k] = $this->penjelasandomainmodel->getPenjelasanDomain($detail['dom'][$k]->DOMAINKE)->result();
//			echo $dom[$k]->DOMAINKE." : ";
//			echo $dom[$k]->NILAIDOMAIN." | ";
			for ($k2 = 0; $k2 < 6; $k2++) {
				$detail['fac'][$k] = $this->PTfacetModel->getPTFacet($detail['dom'][$k]->PTDOMAINID)->result();
				$detail['penjfac'][$k][$k2] = $this->penjelasanfacetmodel->getPenjelasanFacet($detail['fac'][$k][$k2]->FACETKE)->result();
//				echo $fac[$k][$k2]->FACETKE." : ";
//				echo $fac[$k][$k2]->NILAIFACET." | ";
			}
//			$domain[$k] = 
			
		}
//		echo count($fac[0]);
//		echo "FACET = ".$fac[0][0]->FACETKE;
		$this->load->view('penjelasanptpage',$detail);
	}
}
?>